<template>
	<view class="appointmentBody">
		<view class="appointmentTitle">
			<view class="appointmentTitleText">
				<text>购买光伏组件是您加入蜜袋互助的基础，光伏组件的发电收益将用于支撑您的互助保障。</text>
			</view>
		</view>
		<view class="appointmentContent" v-if="appoinmentTime">
			<image src="../../static/image/appointment.png"></image>
			<text>光伏组件暂未开售，请预约！</text>
		</view>
		
		<view class="appointmentNone" v-else>
			<view class="appointmentTitleNone">
				<image src="../../static/image/appointmentSuccess.png"></image>
				<text>预约成功</text>
				<text>{{infos.subscribeTime}}</text>
			</view>
			<view class="appointmentNum">预约数量：{{infos.quantity}}</view>
		</view>

		<view class="appointmentTime">
			<view class="appoinmentTimetitle">
				<text class="appoinmentTimetitleText">距离预约结束：</text>
				<view class="appointmentTimeselect">
					<text class="appointmentTimeDay">{{day}}天</text>
					<view class="appointmentTimeHMS">
						<text>{{hr}}</text>
						<text style="background: none;color: #F3A500;width: 30rpx;"> :</text>
						<text>{{min}}</text>
						<text style="background: none;color: #F3A500;width: 30rpx;"> :</text>
						<text>{{sec}}</text>
					</view>
				</view>
			</view>

			<view class="appoinmentTimeBottom">
				<text>预约将享有<text style="color: #F3A500;">优先购买权</text>，光伏组件开售前，我们将短信通知您购买！</text>
			</view>
		</view>

		<view class="appointmentBtn" @click="immediateAppointment()" v-if="appoinmentTime">
			立即预约
		</view>

		<!-- 预约弹框 -->
		<view class="mask" v-if="modalShow">
			<view class="modalAppoint">
				<view class="modalAppointHead">
					<view class="modalAppointHeadFir">
						<text>预约</text>
						<image src="../../static/image/close.png" mode="" class="close" @click="closeMobileAlert"></image>
					</view>

					<view class="modalAppointHeadSec">
						<text>请输入您要购买的资产数量（限量10份）</text>
					</view>
				</view>


				<view class="moble-form">
					<view class="appointmentAll">
						<text>预约数量</text>
						<view class="appointmentA">
							<view  @click="subtract()">-</view>
							<input type="number" v-model="num" onkeyup="NumberCheck(this)"/>
							<view @click="add()">+</view>
						</view>
					</view>


					<view class="appointment-btn" @click="nextPage()">
						确认预约
					</view>
				</view>
			</view>

		</view>
	</view>
</template>

<script>
	import api from '../../api/index.js'
	export default {
		data() {
			return {
				orderId: '', //订单id
				day: 0, //天
				hr: 0, //时
				min: 0, //分
				sec: 0, //秒
				_interval: null,
				modalShow: false,
				num: 1,
				infos:{},//预约成功的返回信息
				appoinmentTime:'',//是否预约过
				planId:''
			}
		},
		onLoad(opt) {
			this.planId=1;
			this.num=opt.personLength;
		},
		onShow() {
			// 查询预约结果
			this.appoinmentReeult()
		},
		//当离开页面时，清除倒计时
		beforeDestroy() {
			console.log('离开页面')
			clearInterval(this._interval)
		},
		methods: {
			// 立即预约
			immediateAppointment() {
                this.modalShow=true;
			},
			closeMobileAlert(){
				this.modalShow=false;
			},
			//实现数字增加
			add: function(count) {
				if (this.num == 10) {
					return;
				} 
				this.num++;
			},
			//数字减少，如果少于0则弹出不能再少
			subtract: function(count) {
				if (this.num <= 1) {
					return;
				} else {
					this.num -= 1;

				}
			},
			//倒计时
			countdown(finallTime) {
				const format=finallTime.replace(/-/g,'/');
				const end = Date.parse(new Date(format));
				const now = Date.parse(new Date());
				const msec = end - now;
				let day = parseInt(msec / 1000 / 60 / 60 / 24);
				let hr = parseInt(msec / 1000 / 60 / 60 % 24);
				let min = parseInt(msec / 1000 / 60 % 60);
				let sec = parseInt(msec / 1000 % 60);
				this.day = day
				this.hr = hr > 9 ? hr : '0' + hr;
				this.min = min > 9 ? min : '0' + min;
				this.sec = sec > 9 ? sec : '0' + sec;
				const that = this;
				this._interval = setTimeout(function() {
					that.countdown(finallTime)
				}, 1000)
			},
			// 查询结果
			appoinmentReeult(){
				let data={
					planId:this.planId,
					stationId:'',
				}
				api.joinHellp.appoinmentResult(
					data
				).then(res => {
					if (res.code == 1) {
						let appoinmentQuantity=res.data.quantity;
					   if(appoinmentQuantity==undefined){
						   this.appoinmentTime=true
					   }else{
						   this.infos=res.data;
						   this.appoinmentTime=false
					   }
					      this.countdown(res.data.endTime)
					}
				})
			},
			// 确认预约接口
			nextPage(){
				let data={
					quantity: parseInt(this.num),
					planId:this.planId,
					stationId:'',
					remark:''
				}
				api.joinHellp.appoinmentConfirm(
					data
				).then(res => {
					if (res.code == 1) {
					   this.modalShow=false;
					   uni.navigateTo({
					   	url:'/pages/confirmOrder/appointmentSuccess'
					   })
					}
				})
			},
		
		},
	}
</script>

<style>
	@import url("../joinPage/joinPage.css");

	.modalAppoint {
		width: 100%;
		height: 563rpx;
		background: rgba(255, 255, 255, 1);
		border-radius: 60rpx 60rpx 0rpx 0rpx;
		position: fixed;
		bottom: 0;
	}

	.modalAppointHead {
		display: flex;
		flex-direction: column;
		margin-top: 39rpx;
		align-items: center;
	}

	.modalAppointHeadFir {
		display: flex;
		font-size: 34rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: rgba(0, 0, 0, 1);
		justify-content: flex-start;
	}

	.modalAppointHeadFir image {
		width: 22rpx;
		height: 22rpx;
		position: absolute;
		right: 50rpx;
		top: 10rpx;
	}

	.modalAppointHeadSec {
		font-size: 24rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: rgba(153, 153, 153, 1);
		margin: 20rpx auto 40rpx;
	}

	.appointmentAll {
		display: flex;
		width: 670rpx;
		height: 100rpx;
		border-bottom: 1rpx solid #EEEEEE;
		justify-content: space-between;
		align-items: center;
	}

	.appointmentAll text {
		font-size: 28rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: rgba(153, 153, 153, 1);
	}

	.appointmentA {
		display: flex;
		justify-content: flex-start;
	}

	.appointmentA input {
		width: 90rpx;
		height: 50rpx;
		background: rgba(238, 238, 238, 1);
		border-radius: 25rpx;
		text-align: center;
		margin: 0 23rpx;
	}

	.appointment-btn {
		width: 670rpx;
		height: 98rpx;
		background: rgba(123, 187, 56, 1);
		border-radius: 49rpx;
		margin: auto;
		font-size: 40rpx;
		font-family: PingFang SC;
		font-weight: 800;
		color: rgba(255, 255, 255, 1);
		text-align: center;
		line-height: 98rpx;
		margin-top: 61rpx;
	}

	.appointmentTitle {
		width: 100%;
		height: 80rpx;
		background: rgba(255, 246, 234, 1);
		display: flex;
		align-items: center;
	}

	.appointmentTitleText {
		width: 688rpx;
		margin: auto;
		font-size: 24rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: rgba(255, 161, 0, 1);

	}

	.appointmentBody {
		height: 100%;
		background: #FFFFFF;
	}

	.appointmentContent {
		display: flex;
		flex-direction: column;
		align-items: center;
		margin-top: 30rpx;
	}

	.appointmentContent image {
		width: 360rpx;
		height: 360rpx;
		margin-bottom: 30rpx;
	}

	.appointmentContent text {
		font-size: 28rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: rgba(153, 153, 153, 1);
	}

	.appointmentTime {
		width: 690rpx;
		height: 315rpx;
		background: rgba(255, 247, 232, 1);
		border-radius: 30rpx;
		margin: 53rpx auto 83rpx;
	}

	.appoinmentTimetitle {
		height: 188rpx;
		border-bottom: 1px solid #F3A500;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}

	.appoinmentTimetitle .appoinmentTimetitleText {
		font-size: 30rpx;
		font-family: PingFang SC;
		font-weight: bold;
		color: rgba(17, 17, 17, 1);
	}

	.appoinmentTimeBottom {
		width: 585rpx;
		height: 63rpx;
		font-size: 28rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: rgba(153, 153, 153, 1);
		margin: 26rpx auto;
	}

	.appointmentBtn {
		width: 590rpx;
		height: 118rpx;
		background: linear-gradient(0deg, rgba(101, 189, 0, 1), rgba(161, 244, 0, 1));
		border-radius: 59rpx;
		margin: auto;
		font-size: 40rpx;
		font-family: PingFang SC;
		font-weight: bold;
		color: rgba(255, 255, 255, 1);
		line-height: 118rpx;
		text-align: center;
	}

	.appointmentTimeselect {
		display: flex;
		align-items: center;
		margin-top: 18rpx;
	}

	.appointmentTimeselect .appointmentTimeDay {
		font-size: 40rpx;
		font-family: PingFang SC;
		font-weight: 800;
		color: rgba(243, 165, 0, 1);
		margin-right: 9rpx;
	}

	.appointmentTimeHMS {
		font-size: 40rpx;
		font-family: PingFang SC;
		font-weight: 800;
		color: #F4A500;
	}

	.appointmentTimeHMS text {
		display: inline-block;
		width: 60rpx;
		height: 80rpx;
		background: rgba(244, 165, 0, 1);
		border-radius: 10rpx;
		color: rgba(255, 255, 255, 1);
		text-align: center;
		line-height: 80rpx;
	}
	.appointmentNone{
		width:690rpx;
		height:364rpx;
		background:linear-gradient(-34deg,rgba(43,220,171,1),rgba(0,220,114,1),rgba(73,219,135,1));
		border-radius:30rpx;
		margin: 60rpx auto;
	}
	.appointmentTitleNone{
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		height: 264rpx;
		border-bottom: 1rpx solid #FFFFFF;
		font-family:PingFang SC;
		font-weight:bold;
		color:rgba(255,255,255,1);
	}
	.appointmentTitleNone text:first-of-type{
		font-size:30rpx;
		margin: 20rpx 0;
	}
	.appointmentTitleNone text:last-of-type{
		font-size:24rpx;
	}
	.appointmentTitleNone image{
		width: 90rpx;
		height: 90rpx;
	}
	.appointmentNum{
		font-size:30rpx;
		font-family:PingFang SC;
		font-weight:500;
		color:rgba(255,255,255,1);
		height: 99rpx;
		line-height:99rpx;
		text-align: center;
	}
</style>
